package com.cjq.allservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjq.allservice.entity.News;
import com.cjq.allservice.entity.Type;
import com.cjq.allservice.entity.vo.NewsQuery;
import com.cjq.allservice.entity.vo.TypeQuery;
import com.cjq.allservice.service.NewsService;
import com.cjq.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cjq
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/allservice/news")
@CrossOrigin
public class NewsController {

    private final NewsService newsService;

    public NewsController(NewsService newsService){
        this.newsService=newsService;
    }

    @ApiOperation("添加首页公告")
    @PostMapping("/saveNews")
    public R saveNews(@RequestBody News news){
        boolean b = newsService.save(news);
        if (b){
            return R.succ().message("添加公告成功！");
        }else{
            return R.fail().message("添加公告失败！");
        }
    }

    @ApiOperation("进行公告相关信息的修改")
    @PostMapping("/updateNewsById")
    public R updateNewsById(@RequestBody News news){
        boolean b = newsService.updateById(news);
        if (b){
            return R.succ().message("修改公告成功！");
        }else{
            return R.fail().message("修改公告失败！");
        }
    }

    @ApiOperation("根据公告id获取")
    @GetMapping("/getNewsById")
    public R getNewsById(@RequestParam String newsId){
        News news = newsService.getById(newsId);
        news.setNewsViews(news.getNewsViews()+1);
        newsService.updateById(news);
        return R.succ().data("news",news);
    }

    @ApiOperation("获取公告列表进行遍历")
    @GetMapping("/getAllNews")
    public R getAllNews(){
        List<News> list = newsService.list();
        return R.succ().data("newsList",list);
    }

    @ApiOperation("进行公告删除操作")
    @DeleteMapping("/deleteNewsById")
    public R deleteNewsById(@RequestParam String newsId){
        boolean b = newsService.removeById(newsId);
        if (b){
            return R.succ().message("删除公告成功！");
        }else{
            return R.fail().message("删除公告失败！");
        }
    }


    @ApiOperation("条件查询公告带分页")
    //条件查询带分页
    @PostMapping ("pageNewsCondition/{current}/{limit}")
    public R pageNewsCondition(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) NewsQuery newsQuery){
        Page<News> page=new Page<>(current,limit);
        QueryWrapper<News> wrapper = new QueryWrapper<>();

        //多条件组合查询
        String id = newsQuery.getNewsId();
        String name = newsQuery.getNewsTitle();
        String status = newsQuery.getNewsContent();
        if (!StringUtils.isEmpty(id)){
            wrapper.like("news_id",id);
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("news_title",name);
        }
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("news_content",status);
        }
        wrapper.orderByDesc("update_time");

        newsService.page(page,wrapper);
        long total=page.getTotal();
        System.out.println(total);
        List<News> records = page.getRecords();
        HashMap map = new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.succ().data(map);
    }

}

